/**
 * 加载字典集合到select容器中 view-视图容器 lxjp-字典类型 lFilter-lay-filter用于更新渲染 selVal-默认选中字典值
 */
var zdCommon = {
	"initZdListOpts" : function(view, lxjp, selVal, isEmpty, callback) {
		var sUrl = basePath + "/zd/loadZdList";
		var params = {
			"lxjp" : lxjp
		};
		$.ajax({
			"url" : sUrl,
			"type" : "post",
			"async" : false,
			"dataType" : "json",
			"data" : params,
			success : function(resData) {
				if (resData.code == "0") {
					var zdList = resData.data;
					var tplData = {
						"zdList" : zdList,
						"selVal" : selVal
					};
					if (isEmpty) {
						var emptyOpt = $("<option value=''>请选择</option>");
						$(view).append(emptyOpt);
					}
					for ( var i in zdList) {
						var zdI = zdList[i];
						var selOpt = "";
						if (selVal == zdI.zddm) {
							selOpt = "selected='selected'";
						}
						var opt = $("<option value='" + zdI.zddm + "' "
								+ selOpt + ">" + zdI.zdmc + "</option>");
						$(view).append(opt);
					}
					if (callback) {
						callback(resData);
					}
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {

			}
		});
	},
	/**
	 * 加载字典集合到select容器中 zddm-要翻译的字典代码 lxjp-字典类型 isJc-是否简称
	 */
	"getZdmc" : function(lxjp, zddm, isJc) {
		var sUrl = basePath + "/zd/getZdmc";
		var params = {
			"lxjp" : lxjp,
			"zddm" : zddm,
			"isJc" : isJc
		};
		var zdmcVal = "";
		$.ajax({
			"url" : sUrl,
			"type" : "post",
			"async" : false,
			"dataType" : "json",
			"data" : params,
			success : function(resData) {
				if (resData.code == "0") {
					zdmcVal = resData.data;
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {

			}
		});
		return zdmcVal;
	},
	/**
	 * 加载字典集合到select容器中 view-视图容器 lxjp-字典类型 zdz-过滤的字典值（字符串逗号拼接） selVal-默认选中字典值
	 */
	"initZdListFilterOpts" : function(view, lxjp, zdz, selVal, isEmpty, callback) {
		var sUrl = basePath + "/zd/loadZdList";
		var params = {
				"lxjp" : lxjp
		};
		$.ajax({
			"url" : sUrl,
			"type" : "post",
			"async" : true,
			"dataType" : "json",
			"data" : params,
			success : function(resData) {
				if (resData.code == "0") {
					var zdList = resData.data;
					var tplData = {
							"zdList" : zdList,
							"selVal" : selVal
					};
					if (isEmpty) {
						var emptyOpt = $("<option value=''>请选择</option>");
						$(view).append(emptyOpt);
					}
					for (var i in zdList) {
						var zdI = zdList[i];
						var selOpt = "";
						if (selVal == zdI.zddm) {
							selOpt = "selected='selected'";
						}
						if(zdz){
							var zdzArr = zdz.split(",");
							for(var j in zdzArr){
								if(zdzArr[j] == zdI.zddm){
									var opt = $("<option value='" + zdI.zddm + "' "
											+ selOpt + ">" + zdI.zdmc + "</option>");
									$(view).append(opt);
								}
							}
						}else{
							var opt = $("<option value='" + zdI.zddm + "' "
									+ selOpt + ">" + zdI.zdmc + "</option>");
							$(view).append(opt);
						}
					}
					if (callback) {
						callback(resData);
					}
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		});
	},
	"initZdLxjpList" : function(view, selVal, isEmpty, callback) {
		var sUrl = basePath + "/zd/zdlxList";
		$.ajax({
			"url" : sUrl,
			"type" : "post",
			"async" : true,
			"dataType" : "json",
			success : function(resData) {
				if (resData.code == "0") {
					var zdList = resData.data;
					if (isEmpty) {
						var emptyOpt = $("<option value=''>请选择</option>");
						$(view).append(emptyOpt);
					}
					for (var i in zdList) {
						var zdI = zdList[i];
						var selOpt = "";
						if (selVal == zdI.lxjp) {
							selOpt = "selected='selected'";
						}
						var opt = $("<option value='" + zdI.lxjp + "' "
								+ selOpt + ">" + zdI.lxjpmc + "</option>");
						$(view).append(opt);
					}
					if (callback) {
						callback(resData);
					}
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		});
	}
};
//提示弹出框公用方法
var showMsg = {
		/**
		 * 带确定和取消
		 */
		"confirm" : function(msg, okCallback, cancelCallback){
			layer.confirm(msg, {
				btn: ['确定','取消'] 
			}, function(idx){
				if (okCallback) {
					okCallback();
				}
				layer.close(idx);
			}, function(idx){
				if (cancelCallback) {
					cancelCallback();
				}
				layer.close(idx);
			});
		},
		/**
		 * 只带确定的
		 */
		"alert" : function(msg, callback){
			if (callback) {
				layer.alert(msg, function(){
					callback();
				});
			}else{
				layer.alert(msg);
			}
		},
		/**
		 * 没有确定和取消
		 */
		"msg" : function(msg){
			layer.msg(msg);
		},
		/**
		 * 没有确定和取消保留10秒
		 */
		"hold" : function(msg){
			layer.msg(msg, {time: 3000});
		}
};
//window.open
var winOpen = function(url, width, height){
	var top = (window.screen.availHeight-50-height)/2; 
	var left = (window.screen.availWidth-10-width)/2; 
	if(window.screen.availWidth-10<width){
		width = window.screen.availWidth-10;
		left = 0;
	}
	window.open(url,'_blank','width='+width+',height='+height+',top='+top+',left='+left
		+',overflow-x:hidden,overflow-y:scroll,toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=no,status=no');
}
/**表单序列化*/
function fromSerialize(selReg){
	return $(selReg).serializeObject();
}
$.fn.serializeObject = function() {
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
}
/**
 * 字符串格式日期时间格式化（yyyy-MM-dd HH:mm:ss）
 */
function dateFormat(date){
	if(date){
		if(date.length == 8){
			return date.replace(/(\d{4})(\d{2})(\d{2})/g,'$1-$2-$3');
		}else if(date.length == 14){
			return date.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/g,'$1-$2-$3 $4:$5:$6');
		}else{
			return "";
		}
	}else{
		return "";
	}
}
/**
 * 字符串格式日期时间去除格式化
 */
function parseDate(date){
	if(date){
		return date.replace(/-/g,"").replace(/:/g,"").replace(/\s+/g,"");
	}else{
		return "";
	}
}
/**
 * 延迟刷新页面
 * @param url
 * @param time
 * @returns
 */
function windowReload(url,time){
	if(!time){
		time = 2000;
	}
	setTimeout(function(){
		window.location.href = url;
	},time);
}
var stringUtils = {
		"isBlank":function(val){
			var _v = "";
			if(val){
				_v = val.replace(/(^\s*)|(\s*$)/g, "");
				if(_v == ""){
					return true;
				}
			}else{
				return true;
			}
			return false;
		},
		"isNotBlank":function(val){
			var isb = stringUtils.isBlank(val);
			return !isb;
		}
};